using System;
using System.Collections.Generic;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using IWS.Lib.SingleSignOn;
using System.Web.Security;
using IWS.Lib.SingleSignOn.ServiceResponses;


/// <summary>
/// Summary description for RoleService
/// </summary>
[WebService(Namespace = "http://www.integratedwebsystems.com/Services/", Description = "This is the role provider service.")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class RoleService : System.Web.Services.WebService
{

	public RoleService()
	{

		//Uncomment the following line if using designed components 
		//InitializeComponent(); 
	}

	private void LogException(Exception ex)
	{
		if (IWS.Lib.SingleSignOn.Utility.AllowExceptionLogging)
			IWS.Lib.SingleSignOn.Utility.LogException(System.Reflection.Assembly.GetExecutingAssembly(), ex);
	}

	private bool ValidateServiceKey(string sServiceKey)
	{
		if (IWS.Lib.SingleSignOn.Service.Exists(sServiceKey))
		{
			IWS.Lib.SingleSignOn.Service svc = new Service(sServiceKey);
			if (svc.ExpirationDate >= DateTime.Now.ToUniversalTime())
			{
				svc.LastActiveDate = DateTime.Now.ToUniversalTime();
				svc.Save();
				return true;
			}
		}
		return false;
	}

	[WebMethod]
	public ServiceResponse AddUsersToRoles(string sServiceKey, string[] usernames, string[] roleNames)
	{
		ServiceResponse resp = new ServiceResponse();
		try
		{
			if (ValidateServiceKey(sServiceKey))
			{
				Roles.Provider.AddUsersToRoles(usernames, roleNames);
				resp.Result = ServiceResponseBase.eResult.Success;
			}
			else
			{
				resp.Result = ServiceResponseBase.eResult.InvalidRegistration;
			}
		}
		catch (Exception ex)
		{
			resp.Message = ex.Message;
			resp.Result = ServiceResponseBase.eResult.Error;

			this.LogException(ex);
		}
		return resp;
	}

	[WebMethod]
	public ServiceResponse GetApplicationName(string sServiceKey)
	{
		ServiceResponse resp = new ServiceResponse();
		try
		{
			if (ValidateServiceKey(sServiceKey))
			{
				resp.Data = Roles.Provider.ApplicationName;
				resp.Result = ServiceResponseBase.eResult.Success;
			}
			else
			{
				resp.Result = ServiceResponseBase.eResult.InvalidRegistration;
			}
		}
		catch (Exception ex)
		{
			resp.Message = ex.Message;
			resp.Result = ServiceResponseBase.eResult.Error;

			this.LogException(ex);
		}
		return resp;
	}
	[WebMethod]
	public ServiceResponse SetApplicationName(string sServiceKey, string name)
	{
		ServiceResponse resp = new ServiceResponse();
		try
		{
			if (ValidateServiceKey(sServiceKey))
			{
				Roles.Provider.ApplicationName = name;
				resp.Result = ServiceResponseBase.eResult.Success;
			}
			else
			{
				resp.Result = ServiceResponseBase.eResult.InvalidRegistration;
			}
		}
		catch (Exception ex)
		{
			resp.Message = ex.Message;
			resp.Result = ServiceResponseBase.eResult.Error;

			this.LogException(ex);
		}
		return resp;
	}

	[WebMethod]
	public ServiceResponse CreateRole(string sServiceKey, string roleName)
	{
		ServiceResponse resp = new ServiceResponse();
		try
		{
			if (ValidateServiceKey(sServiceKey))
			{
				Roles.Provider.CreateRole(roleName);
				resp.Result = ServiceResponseBase.eResult.Success;
			}
			else
			{
				resp.Result = ServiceResponseBase.eResult.InvalidRegistration;
			}
		}
		catch (Exception ex)
		{
			resp.Message = ex.Message;
			resp.Result = ServiceResponseBase.eResult.Error;

			this.LogException(ex);
		}
		return resp;
	}

	[WebMethod]
	public ServiceResponse DeleteRole(string sServiceKey, string roleName, bool throwOnPopulatedRole)
	{
		ServiceResponse resp = new ServiceResponse();
		try
		{
			if (ValidateServiceKey(sServiceKey))
			{
				resp.Data = Roles.Provider.DeleteRole(roleName, throwOnPopulatedRole);
				resp.Result = ServiceResponseBase.eResult.Success;
			}
			else
			{
				resp.Result = ServiceResponseBase.eResult.InvalidRegistration;
			}
		}
		catch (Exception ex)
		{
			resp.Message = ex.Message;
			resp.Result = ServiceResponseBase.eResult.Error;

			this.LogException(ex);
		}
		return resp;
	}

	[WebMethod]
	public ArrayServiceResponse FindUsersInRole(string sServiceKey, string roleName, string usernameToMatch)
	{
		ArrayServiceResponse resp = new ArrayServiceResponse();
		try
		{
			if (ValidateServiceKey(sServiceKey))
			{
				resp.Data = Roles.Provider.FindUsersInRole(roleName, usernameToMatch);
				resp.Result = ServiceResponseBase.eResult.Success;
			}
			else
			{
				resp.Result = ServiceResponseBase.eResult.InvalidRegistration;
			}
		}
		catch (Exception ex)
		{
			resp.Message = ex.Message;
			resp.Result = ServiceResponseBase.eResult.Error;

			this.LogException(ex);
		}
		return resp;
	}

	[WebMethod]
	public ArrayServiceResponse GetAllRoles(string sServiceKey)
	{
		ArrayServiceResponse resp = new ArrayServiceResponse();
		try
		{
			if (ValidateServiceKey(sServiceKey))
			{
				resp.Data = Roles.Provider.GetAllRoles();
				resp.Result = ServiceResponseBase.eResult.Success;
			}
			else
			{
				resp.Result = ServiceResponseBase.eResult.InvalidRegistration;
			}
		}
		catch (Exception ex)
		{
			resp.Message = ex.Message;
			resp.Result = ServiceResponseBase.eResult.Error;

			this.LogException(ex);
		}
		return resp;
	}

	[WebMethod]
	public ArrayServiceResponse GetRolesForUser(string sServiceKey, string username)
	{
		ArrayServiceResponse resp = new ArrayServiceResponse();
		try
		{
			if (ValidateServiceKey(sServiceKey))
			{
				resp.Data = Roles.Provider.GetRolesForUser(username);
				resp.Result = ServiceResponseBase.eResult.Success;
			}
			else
			{
				resp.Result = ServiceResponseBase.eResult.InvalidRegistration;
			}
		}
		catch (Exception ex)
		{
			resp.Message = ex.Message;
			resp.Result = ServiceResponseBase.eResult.Error;

			this.LogException(ex);
		}
		return resp;
	}

	[WebMethod]
	public ArrayServiceResponse GetUsersInRole(string sServiceKey, string roleName)
	{
		ArrayServiceResponse resp = new ArrayServiceResponse();
		try
		{
			if (ValidateServiceKey(sServiceKey))
			{
				resp.Data = Roles.Provider.GetUsersInRole(roleName);
				resp.Result = ServiceResponseBase.eResult.Success;
			}
			else
			{
				resp.Result = ServiceResponseBase.eResult.InvalidRegistration;
			}
		}
		catch (Exception ex)
		{
			resp.Message = ex.Message;
			resp.Result = ServiceResponseBase.eResult.Error;

			this.LogException(ex);
		}
		return resp;
	}

	[WebMethod]
	public ServiceResponse IsUserInRole(string sServiceKey, string username, string roleName)
	{
		ServiceResponse resp = new ServiceResponse();
		try
		{
			if (ValidateServiceKey(sServiceKey))
			{
				resp.Data = Roles.Provider.IsUserInRole(username, roleName);
				resp.Result = ServiceResponseBase.eResult.Success;
			}
			else
			{
				resp.Result = ServiceResponseBase.eResult.InvalidRegistration;
			}
		}
		catch (Exception ex)
		{
			resp.Message = ex.Message;
			resp.Result = ServiceResponseBase.eResult.Error;

			this.LogException(ex);
		}
		return resp;
	}

	[WebMethod]
	public ServiceResponse RemoveUsersFromRoles(string sServiceKey, string[] usernames, string[] roleNames)
	{
		ServiceResponse resp = new ServiceResponse();
		try
		{
			if (ValidateServiceKey(sServiceKey))
			{
				Roles.Provider.RemoveUsersFromRoles(usernames, roleNames);
				resp.Result = ServiceResponseBase.eResult.Success;
			}
			else
			{
				resp.Result = ServiceResponseBase.eResult.InvalidRegistration;
			}
		}
		catch (Exception ex)
		{
			resp.Message = ex.Message;
			resp.Result = ServiceResponseBase.eResult.Error;

			this.LogException(ex);
		}
		return resp;
	}

	[WebMethod]
	public ServiceResponse RoleExists(string sServiceKey, string roleName)
	{
		ServiceResponse resp = new ServiceResponse();
		try
		{
			if (ValidateServiceKey(sServiceKey))
			{
				Roles.Provider.RoleExists(roleName);
				resp.Result = ServiceResponseBase.eResult.Success;
			}
			else
			{
				resp.Result = ServiceResponseBase.eResult.InvalidRegistration;
			}
		}
		catch (Exception ex)
		{
			resp.Message = ex.Message;
			resp.Result = ServiceResponseBase.eResult.Error;

			this.LogException(ex);
		}
		return resp;
	}
}

